package com.putao.tinytime.application;

import android.app.Application;
import android.content.Context;

import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
import com.putao.tinytime.util.DisplayHelper;
import com.putao.tinytime.util.FileOperationHelper;
import com.putao.tinytime.util.PuTaoConstants;
import com.putao.tinytime.util.SharedPreferencesHelper;
import com.putao.tinytime.util.StringHelper;

/**
 * Created by jidongdong on 15/3/19.
 */
public class GlobalApplication extends Application {
    private static Context globalContext;

    @Override
    public void onCreate() {
        super.onCreate();
        globalContext = getApplicationContext();
        DisplayHelper.init(this);
        ImageLoader.getInstance().init(ImageLoaderConfiguration.createDefault(globalContext));
        boolean isFristUse = SharedPreferencesHelper.readBooleanValue(this, PuTaoConstants.PREFERENC_FIRST_USE_APPLICATION, true);
        if (isFristUse) {
            doFirstInitCollageFileCopy();
        }
    }

    public static Context getInstance() {
        return globalContext;
    }


    public void doFirstInitCollageFileCopy() {
        String res_name = PuTaoConstants.COLLAGE_FLODER_NAME + ".zip";
        boolean bSuccess = FileOperationHelper.copyAssetsFileToExternalFile(res_name);
        if (bSuccess) {
            try {
                FileOperationHelper.unZipFile(res_name);
//                File file = new File(res_name);
//                if (file.exists()) {
//                    file.delete();
//                }
                String config_str = FileOperationHelper.readJsonFile(PuTaoConstants.COLLAGE_FLODER_NAME, PuTaoConstants.COLLAGE_CONFIG_NAME);
//                Loger.d("config_str------>" + StringHelper.replaceBlank(config_str));
                SharedPreferencesHelper.saveStringValue(this, PuTaoConstants.PREFERENC_COLLAGE_CONFIG_JSON, StringHelper.replaceBlank(config_str));
                SharedPreferencesHelper.saveIntValue(this, PuTaoConstants.PREFERENC_COLLAGE_SRC_VERSION_CODE, 1);
                SharedPreferencesHelper.saveBooleanValue(this, PuTaoConstants.PREFERENC_FIRST_USE_APPLICATION, false);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
}
